<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

/**
 * Controller Test case
 *
 * @category Tests
 */
abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    /**
     * Application entity
     *
     * @var Zend_Application
     */
    protected $_application;

    /**
     * fixtures put here
     *
     * @var as you wish
     */
    protected $_fixture;

    /**
     * Setup TestCase
     */
    public function setUp()
    {
        $this->bootstrap = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );

        parent::setUp();

        $bootstrap = $this->bootstrap->getBootstrap();
        $this->getFrontController()->setParam('bootstrap', $bootstrap);
    }

    /**
     * Init Application
     */
    static public function appInit()
    {
        $application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );

        $application->bootstrap();
    }

    /**
     * Shut down Application
     */
    static public function appDown()
    {
    }

    /**
     * debug
     *
     * @return string
     */
    public function debug()
    {
        var_dump($this->_response->getBody());
//        if ($this->getResponse()->isException()) {
//            echo "Exceptions: \n";
//            print_r($this->getResponse()->getException());
//        }
    }

    /**
     * Change environment for member role/status
     * Should be run after setUp() !!!
     *
     * @param string $role
     * @param string $status
     * @return void
     */
    protected function _doLogin($role = Model_Member::ROLE_MEMBER, $status = Model_Member::STATUS_ACTIVE)
    {
        Zend_Auth::getInstance()
            ->getStorage()
            ->write(self::_generateFakeIdentity($role, $status))
        ;
    }


    /**
     * Create member
     *
     * @param string $role
     * @param string $status
     * @return StdClass an identity
     */
    protected function _generateFakeIdentity($role = Model_Member::ROLE_MEMBER, $status = Model_Member::STATUS_ACTIVE)
    {
        $account = new stdClass();

        $account->id       = 1;
        $account->email    = 'autotest' . time() . '@example.org';
        $account->password = sha1('password');
        $account->username = 'AutoTest' . date('YmdHis');
        $account->role     = $role;
        $account->status   = $status;

        return $account;
    }

    /**
     * Remove environment
     *
     */
    protected function tearDown()
    {
        Zend_Controller_Front::getInstance()->resetInstance();
        $this->resetRequest();
        $this->resetResponse();

        $this->request->setPost(array());
        $this->request->setQuery(array());

        $dbAdapter = Zend_Db_Table_Abstract::getDefaultAdapter();
        $dbAdapter->closeConnection();

        parent::tearDown();
    }
}